<?php 
    parse_str($argv[1], $_GET);

    if (isset($_GET['input'])) {
        $input = $_GET['input'];
    } 

    $cmd = "/usr/bin/top -b -n1"; 

    $idle = "";
    if (isset($_GET['idle'])) { 
	$idle = "checked";
	$cmd  .= " -i ";
    }

    $threads = "";
    if (isset($_GET['threads'])) { 
	$threads = "checked";
	$cmd  .= " -H ";
    }

?>

<STYLE TYPE="text/css">
<!--
TD { padding: 0; spacing: 0: height: 0; font-family: monospace; white-space: pre; }
TR { padding: 0; spacing: 0: height: 0; font-family: monospace; white-space: pre; }
# td.odd  { background: #eef; }
# td.even { background: #ccf; }
th       { background: #b8d0f8; }
td.odd   { background: #f0f0f0; }
td.even  { background: #ffffff; }
td.ident { background: #b8d0f8; }
td.top   { background: #FFFFFF; }
--->
</STYLE>

<P>
<CENTER>
<table width='100%' border=0 cellspacing=0 cellpadding=0>
  <tr bgcolor='#bbbbbb' align='center'>
   <form action="<?=$path;?>" method="get">
    <p>
      <input type="text" name="input" value="<?=$input;?>"/>
      <input type="checkbox" name="idle" <? echo "$idle"; ?> >Idle
      <input type="checkbox" name="threads" <? echo "$threads" ?> >Threads
      <input type="submit" value="grep" />
    </p>
   </form>
 </tr>
</table>
</P>
</CENTER>
<CENTER>
<table width='100%' border=0 cellspacing=0 cellpadding=0>

<?
     $linecount = 0;
     $style = array("even","odd","top","ident");
     $ix    = 0;

     $pfh = popen("$cmd", "r");
     while ( !feof($pfh) ) {
	$linecount++;
	$line = fgets($pfh); 
	$line = rtrim($line);
        if ( $linecount <  7 ) { $ix = 2; }
        if ( $linecount == 7 ) { $ix = 3; }
        if ( $linecount >  7 ) { 
	   if ( $ix > 1 ) { $ix = 1; } 
	   if ( isset($input) && strlen($input) > 0 ) {
	        if ( !eregi($input,$line) ) { 
		   continue; 
		}
	   }
           $ix   = $ix ^ 1;
        }
        print " <tr>\n  <td class='$style[$ix]'>$line</td>\n </tr>\n";
     }
     pclose($pfh);

?>
</table>
</CENTER>
